// bluebcastDlg.cpp : implementation file
//

#include "stdafx.h"
#include "bluebcast.h"
#include "bluebcastDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CbluebcastDlg dialog




CbluebcastDlg::CbluebcastDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CbluebcastDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CbluebcastDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CbluebcastDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


// CbluebcastDlg message handlers

BOOL CbluebcastDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	if ( !InitBluetooth() )
	{
		TRACE(_T("InitBluetooth failed.\n"));
		return TRUE;
	}

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CbluebcastDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CbluebcastDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

bool 
CbluebcastDlg::InitBluetooth()
{
	if ( AfxBeginThread(InitBluetoothProc, NULL) == NULL )
	{
		TRACE(_T("Failed to create new thread to start bluetooth.\n"));
		return false;
	}

	return true;
}

void
CbluebcastDlg::ExitBluetooth()
{
	if ( BTSDK_OK != Btsdk_StopBluetooth() )
	{
		TRACE(_T("Btsdk_StopBluetooth() failed.\n"));
	}

	UnregBluetoothEvent();
	Btsdk_Done();
}

UINT 
CbluebcastDlg::InitBluetoothProc(void* arg)
{
	if ( BTSDK_TRUE != Btsdk_IsSDKInitialized() )
	{
		if ( BTSDK_OK != Btsdk_Init() )
		{
			TRACE(_T("Btsdk_Init() failed.\n"));
			InitBluetoothComplete(false);
			return 0;
		}
	}

	if ( BTSDK_TRUE != Btsdk_IsServerConnected() )
	{
		TRACE(_T("Bluetooth server is not connected.\n"));
		InitBluetoothComplete(false);
		return 0;
	}

	// Register callback
	RegBluetoothEvent();

	if ( BTSDK_OK != Btsdk_StartBluetooth() )
	{
		TRACE(_T("Btsdk_StartBluetooth() failed.\n"));
		UnregBluetoothEvent();
		Btsdk_Done();
		InitBluetoothComplete(false);
		return 0;
	}

	// Must
	Btsdk_SetLocalDeviceClass(BTSDK_COMPCLS_DESKTOP);

	InitBluetoothComplete(true);
	return 0;
}

void
CbluebcastDlg::RegBluetoothEvent()
{
	BtSdkCallbackStru cb;

	cb.type = BTSDK_INQUIRY_RESULT_IND;
	cb.func = bt_ind_inquiry_result;
	Btsdk_RegisterCallback4ThirdParty(&cb);

	cb.type = BTSDK_INQUIRY_COMPLETE_IND;
	cb.func = bt_ind_inquiry_complete;
	Btsdk_RegisterCallback4ThirdParty(&cb);

	cb.type = BTSDK_CONNECTION_EVENT_IND;
	cb.func = bt_ind_connection_event;
	Btsdk_RegisterCallback4ThirdParty(&cb);

	cb.type = BTSDK_PIN_CODE_IND;
	cb.func = bt_ind_pin_code;
	Btsdk_RegisterCallback4ThirdParty(&cb);

	cb.type = BTSDK_AUTHORIZATION_IND;
	cb.func = bt_ind_authorization;
	Btsdk_RegisterCallback4ThirdParty(&cb);

	cb.type = BTSDK_LINK_KEY_NOTIF_IND;
	cb.func = bt_ind_link_key_notif;
	Btsdk_RegisterCallback4ThirdParty(&cb);

	cb.type = BTSDK_AUTHENTICATION_FAIL_IND;
	cb.func = bt_ind_authentication_fail;
	Btsdk_RegisterCallback4ThirdParty(&cb);

	Btsdk_RegisterGetStatusInfoCB4ThirdParty(bt_event_status_change);
}

void
CbluebcastDlg::UnregBluetoothEvent()
{
	BtSdkCallbackStru cb;

	cb.type = BTSDK_INQUIRY_RESULT_IND;
	cb.func = NULL;
	Btsdk_RegisterCallback4ThirdParty(&cb);

	cb.type = BTSDK_INQUIRY_COMPLETE_IND;
	cb.func = NULL;
	Btsdk_RegisterCallback4ThirdParty(&cb);

	cb.type = BTSDK_CONNECTION_EVENT_IND;
	cb.func = NULL;
	Btsdk_RegisterCallback4ThirdParty(&cb);

	cb.type = BTSDK_PIN_CODE_IND;
	cb.func = NULL;
	Btsdk_RegisterCallback4ThirdParty(&cb);

	cb.type = BTSDK_AUTHORIZATION_IND;
	cb.func = NULL;
	Btsdk_RegisterCallback4ThirdParty(&cb);

	cb.type = BTSDK_LINK_KEY_NOTIF_IND;
	cb.func = NULL;
	Btsdk_RegisterCallback4ThirdParty(&cb);

	cb.type = BTSDK_AUTHENTICATION_FAIL_IND;
	cb.func = NULL;
	Btsdk_RegisterCallback4ThirdParty(&cb);

	Btsdk_RegisterGetStatusInfoCB4ThirdParty(NULL);
}

void 
CbluebcastDlg::InitBluetoothComplete(bool bComplete)
{
	bComplete ? 
		TRACE(_T("InitBluetoothComplete: TRUE\n")) :
		TRACE(_T("InitBluetoothComplete: FALSE"));


}

void 
CbluebcastDlg::bt_event_status_change(ULONG msMsgType, ULONG pulData, ULONG param, BTUINT8 *arg)
{

}

void 
CbluebcastDlg::bt_ind_inquiry_result(BTDEVHDL device_handle)
{

}

void 
CbluebcastDlg::bt_ind_inquiry_complete()
{

}

void 
CbluebcastDlg::bt_ind_connection_event(BTCONNHDL connection_handle, BTUINT16 *event, BTUINT8 *arg)
{

}

BTUINT8 
CbluebcastDlg::bt_ind_pin_code(BTDEVHDL device_handle)
{

	return 0;
}

BTUINT8 
CbluebcastDlg::bt_ind_authorization(BTSVCHDL svc_handle, BTDEVHDL device_handle)
{

	return 0;
}

void 
CbluebcastDlg::bt_ind_link_key_notif(BTDEVHDL device_handle, BTUINT8 *link_key)
{

}

void 
CbluebcastDlg::bt_ind_authentication_fail(BTDEVHDL device_handle)
{

}

